package org.small.shop.phone.api.process.addr.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.small.shop.phone.api.process.addr.pojo.TblWxAddrVo;
import org.small.shop.phone.api.process.addr.service.TblWxAddrService;
import org.small.shop.phone.api.utils.AppConstantUtils;
import org.small.shop.phone.api.utils.ShopJSONResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TblWxAddrController {

	private static Logger logger = LoggerFactory.getLogger(TblWxAddrController.class);

	@Resource
	private TblWxAddrService tblWxAddrService;

	@PostMapping("/login/getAllAddrs")
	public ShopJSONResult getAllAddrs(HttpServletRequest request) {

		String appId = AppConstantUtils.getAppId(request);
		if (!StringUtils.isEmpty(appId)) {
			try {

				List<TblWxAddrVo> addrVoList = tblWxAddrService.getAllAddrsByUserId(appId);
				if (addrVoList != null && !addrVoList.isEmpty()) {
					return ShopJSONResult.ok(addrVoList);
				}
			} catch (Exception e) {
				logger.info(e.getMessage());
			}
		}
		return ShopJSONResult.errorMsg("查询发货地址失败！");
	}

	@PostMapping("/login/setDefaultAddr")
	public ShopJSONResult getAllAddrs(HttpServletRequest request, Integer addrId) {

		String appId = AppConstantUtils.getAppId(request);
		if (!StringUtils.isEmpty(appId) && addrId != null) {
			try {
				tblWxAddrService.setDefaultAddrByAddrId(appId, addrId);
				return ShopJSONResult.ok("");
			} catch (Exception e) {
				logger.info(e.getMessage());
			}
		}
		return ShopJSONResult.errorMsg("默认发货地址设置失败！");
	}

	@PostMapping("/login/deleteAddr")
	public ShopJSONResult deleteAddr(HttpServletRequest request, Integer addrId) {

		String appId = AppConstantUtils.getAppId(request);
		if (!StringUtils.isEmpty(appId) && addrId != null) {
			try {
				tblWxAddrService.deleteAddrById(addrId, appId);
				return ShopJSONResult.ok("");
			} catch (Exception e) {
				logger.info(e.getMessage());
			}
		}
		return ShopJSONResult.errorMsg("删除发货地址失败！");
	}

	@PostMapping("/login/getAddr")
	public ShopJSONResult getAddr(Integer addrId) {

		if (addrId != null) {
			try {
				TblWxAddrVo addrVo = tblWxAddrService.getAddrsByaddrId(addrId);
				if (addrVo != null) {
					return ShopJSONResult.ok(addrVo);
				}
			} catch (Exception e) {
				logger.info(e.getMessage());
			}
		}
		return ShopJSONResult.errorMsg("获取发货地址详情失败！");
	}

	@PostMapping("/login/addOrUpdateAddr")
	public ShopJSONResult addOrUpdateAddr(TblWxAddrVo addrVo, HttpServletRequest request) {

		String appId = AppConstantUtils.getAppId(request);
		if (!StringUtils.isEmpty(appId)) {
			try {
				if (addrVo.getAddrId() != null) {
					tblWxAddrService.updateAddr(appId, addrVo);
				} else {
					tblWxAddrService.addAddr(appId, addrVo);
				}
				return ShopJSONResult.ok("");
			} catch (Exception e) {
				logger.info(e.getMessage());
			}
		}
		return ShopJSONResult.errorMsg("添加/修改发货地址失败！");
	}
}
